<html>
<head><meta charset="utf-8"><title>Weird type error w/ DefDatabase &amp; HirDatabase- help? · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Weird.20type.20error.20w.2F.20DefDatabase.20.26.20HirDatabase-.20help.3F.html">Weird type error w/ DefDatabase &amp; HirDatabase- help?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233623486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Weird%20type%20error%20w/%20DefDatabase%20%26%20HirDatabase-%20help%3F/near/233623486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jade <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Weird.20type.20error.20w.2F.20DefDatabase.20.26.20HirDatabase-.20help.3F.html#233623486">(Apr 08 2021 at 09:14)</a>:</h4>
<p>I have a DeclValidator that I want to use its DB reference to do queries with, which should be ok I think, since it's a <code>HirDatabase</code>, and <code>trait HirDatabase : DefDatabase</code>, but for some reason (lifetimes and variance shenanigans, probably), rustc is not taking it. Can someone help?</p>
<p>In particular I have a <code>m</code> which is a <code>ModuleId</code>, which I am calling <code>.containing_module(db)</code> on. <code>db</code> is a <code>db: &amp;'a dyn HirDatabase,</code>. I have the full function below if that's helpful.</p>
<div class="codehilite"><pre><span></span><code>error[E0308]: mismatched types
  --&gt; crates/hir_ty/src/diagnostics/decl_check.rs:99:63
   |
99 |                 AttrDefId::ModuleId(m) =&gt; m.containing_module(self.db).map(|v| v.into()),
   |                                                               ^^^^^^^ expected trait `DefDatabase`, found trait `HirDatabase`
   |
   = note: expected reference `&amp;dyn DefDatabase`
              found reference `&amp;&#39;a (dyn HirDatabase + &#39;a)`
</code></pre></div>
<p>The entire function from <code>crates/hir_ty/src/diagnostics/decl_check.rs</code></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="sd">/// Checks whether not following the convention is allowed for this item.</span>
<span class="w">    </span><span class="sd">///</span>
<span class="w">    </span><span class="sd">/// Currently this method doesn't check parent attributes.</span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">allowed</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">id</span>: <span class="nc">AttrDefId</span><span class="p">,</span><span class="w"> </span><span class="n">allow_name</span>: <span class="kp">&amp;</span><span class="kt">str</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">is_allowed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">|</span><span class="n">def_id</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="bp">self</span><span class="p">.</span><span class="n">db</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">attrs</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">by_key</span><span class="p">(</span><span class="s">"allow"</span><span class="p">)</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">tt_values</span><span class="p">()</span><span class="w"></span>
<span class="w">                </span><span class="p">.</span><span class="n">any</span><span class="p">(</span><span class="o">|</span><span class="n">tt</span><span class="o">|</span><span class="w"> </span><span class="n">tt</span><span class="p">.</span><span class="n">to_string</span><span class="p">().</span><span class="n">contains</span><span class="p">(</span><span class="n">allow_name</span><span class="p">))</span><span class="w"></span>
<span class="w">        </span><span class="p">};</span><span class="w"></span>

<span class="w">        </span><span class="n">is_allowed</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="o">||</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">id</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">ModuleId</span><span class="p">(</span><span class="n">m</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">m</span><span class="p">.</span><span class="n">containing_module</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">db</span><span class="p">).</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">v</span><span class="o">|</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="n">into</span><span class="p">()),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">FieldId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">AdtId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">FunctionId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">EnumVariantId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">StaticId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">ConstId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">TraitId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">TypeAliasId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">MacroDefId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">ImplId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">                </span><span class="n">AttrDefId</span>::<span class="n">GenericParamId</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">todo!</span><span class="p">(),</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">map</span><span class="p">(</span><span class="o">|</span><span class="n">mid</span><span class="o">|</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">allowed</span><span class="p">(</span><span class="n">mid</span><span class="p">,</span><span class="w"> </span><span class="n">allow_name</span><span class="p">))</span><span class="w"></span>
<span class="w">            </span><span class="p">.</span><span class="n">unwrap_or</span><span class="p">(</span><span class="kc">false</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="233623785"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Weird%20type%20error%20w/%20DefDatabase%20%26%20HirDatabase-%20help%3F/near/233623785" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Weird.20type.20error.20w.2F.20DefDatabase.20.26.20HirDatabase-.20help.3F.html#233623785">(Apr 08 2021 at 09:16)</a>:</h4>
<p>you want to upcast the database <code>m.containing_module(self.db.upcast()).map(|v| v.into())</code> should work</p>



<a name="233624121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Weird%20type%20error%20w/%20DefDatabase%20%26%20HirDatabase-%20help%3F/near/233624121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jade <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Weird.20type.20error.20w.2F.20DefDatabase.20.26.20HirDatabase-.20help.3F.html#233624121">(Apr 08 2021 at 09:19)</a>:</h4>
<p>Thanks! Is there anywhere I can read into why that trait is required (for learning reasons)? For some reason putting the <code>&amp;*</code> in the function call itself doesn't do it, so there seems to be some kind of special thing going on here?</p>



<a name="233624618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Weird%20type%20error%20w/%20DefDatabase%20%26%20HirDatabase-%20help%3F/near/233624618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Weird.20type.20error.20w.2F.20DefDatabase.20.26.20HirDatabase-.20help.3F.html#233624618">(Apr 08 2021 at 09:23)</a>:</h4>
<p>Rust doesn't allow going from one trait object to the traits "super trait object" because vtables arent defined in such a way (yet?) to my knowledge, so this upcast trait is a trick to get that behavior.<br>
I think this is the relevant rfc issue <a href="https://github.com/rust-lang/rfcs/issues/2765#issuecomment-257878042">https://github.com/rust-lang/rfcs/issues/2765#issuecomment-257878042</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>